a,k=input().split(' ')
f=[[0]*10 for i in range(10)]
for i in range(int(k)):
    x,y=input().split(' ')
    f[int(x)][int(y)]=1
# floyd
for k in range(10):
    for i in range(10):
        f[i][i]=1
        for j in range(10):
            if f[i][k] and f[k][j]:
                f[i][j]=1

cnt=[0]*10
for i in range(10):
    t=0
    for j in range(10):
        if f[i][j]==1:
            t+=1
    cnt[i]=t
ans=1
for ai in a:
    ans=ans*cnt[int(ai)]
print(ans)

